//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

//#define USING_BAT_API

#include "vmodem.h"

#ifdef USING_BAT_API
#include "batimp.h"
#else
#include "aciimp_cmd.h"
#include "aciimp_rsp.h"
#endif // USING_BAT_API

#include "tapierrs.h"
#include "debug.h"

ECode VModem::Initialize()
{
#ifdef USING_BAT_API
    init_BAT();
#else
    init_ACI_response();
#endif // USING_BAT_API
    return NOERROR;
}

ECode VModem::Read(
    /* [in] */ Int64 u64Offset,
    /* [in] */ Int32 bytesToRead,
    /* [out] */ MemoryBuf * pBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    if (pBuffer == NULL ||
        (int)bytesToRead > pBuffer->GetCapacity() ||
        0 == bytesToRead) {
        return E_INVALID_ARGUMENT;
    }

    //WaitResult wr;
    //unsigned char * buf = (unsigned char *) pBuffer->GetPayload();
    return CALLACI_getIndicatorMessage(pBuffer);
}

ECode VModem::Write(
    /* [in] */ Int64 u64Offset,
    /* [in] */ const MemoryBuf & buffer,
    /* [out] */ Int32 * pBytesWritten,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    if (buffer.IsNullOrEmpty() ||
        NULL == pBytesWritten) {
        return E_INVALID_ARGUMENT;
    }
    ECode ec = NOERROR;
    *pBytesWritten = 0;
    return ec;
}

ECode VModem::Control(
    /* [in] */ Handle32 nControlCode,
    /* [in] */ const MemoryBuf & inBuffer,
    /* [out] */ MemoryBuf * pOutBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    ECode ec  = NOERROR;

    VM_LOG("VM: CALLACI_parseAtCommand ->ioctrl.%x in\n", nControlCode);

#ifdef USING_BAT_API
    ec = CALLBAT_parseAtCommand(nControlCode, inBuffer, pOutBuffer);
#else
    ec = CALLACI_parseAtCommand(nControlCode, inBuffer, pOutBuffer);
#endif // USING_BAT_API

    VM_LOG("VM: CALLACI_parseAtCommand ->ioctrl.%x out ec.0x%08x\n", nControlCode, ec);
    return ec;
}

void VModem::Dispose()
{
}

static VModem s_VModem;

EXTERN IDeviceDriver * CDECL CreateVModem(uint_t uDeviceNo, void *pvParameter)
{
    ECode ec = s_VModem.Initialize();
    if (FAILED(ec)) { return NULL; }
    s_VModem.AddRef();
    return &s_VModem;
}

